package com.itheima.map;
import java.util.*;

/*
Map<K,V>：
    k--键
    v--值

    键值对对象：
        不能包含重复的键，每个键最多映射一个值

HashMap；
    键唯一依赖的时hashCode方法和equals方法
TreeMap：
    键唯一依赖的红黑树，对键进行排序

    HashSet使用的是HashMap的键

常用api：
    V put(K key,V value)添加元素
        --如果集合中没有该键，添加，返回null
        --如果集合中有该键，修改，返回被替换的值
    V remove​(Object key)
        --根据键删除该键值对，返回值
    int size​()
        --键值对的对数
    boolean containsKey​(Object key)
        --判断是否包含指定的键

    遍历方式：
        通过键找值的方式
        通过键值对对象找键和值



 */
public class Demo_Map {
    public static void main(String[] args) {
//        Map<String,String> map = new HashMap<>();
//        //V put(K key,V value)添加元素
//        map.put("001","赤兔");
//        map.put("002","的卢");
//        map.put("003","绝影");
//        map.put("004","乌骓");
//        map.put("005","爪黄飞电");
        //常用api
        /*System.out.println(map);
        map.remove("heima005");
        System.out.println(map);
        map.clear();
        System.out.println(map);
        System.out.println(map.containsKey("heima002"));
        System.out.println(map.containsKey("heima006"));
        System.out.println(map.containsValue("乌骓"));
        System.out.println(map.containsValue("赤焰"));
        System.out.println(map.isEmpty());*/
        //遍历
        /*//通过键找值的方式遍历
        //获取所有的键 Set<K> keySet​()
        Set<String> keys = map.keySet();
        //遍历set集合得到每一个键
        for (String key : keys) {
            //通过键获取值
            String value = map.get(key);
            System.out.println(key+"=="+value);
        //获取所有键值对对象的集合
        //Entry里面装的是键和值
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            //找键
            String key = entry.getKey();
            //找值
            String value = entry.getValue();
            System.out.println(key+"=="+value);
        }*/
        //HashMap集合
//        HashMap<Student,String> map = new HashMap<>();
//        Student s1 = new Student("太上",500);
//        Student s2 = new Student("元始",400);
//        Student s3 = new Student("通天",300);
//        map.put(s1,"八景宫");
//        map.put(s2,"玉虚宫");
//        map.put(s3,"碧游宫");
        //遍历
        /*Set<Student> keySet = map.keySet();
        for (Student key : keySet) {
            String value = map.get(key);
            System.out.println(key+"---"+value);
        }
        System.out.println("---------------------------");
        Set<Map.Entry<Student, String>> entries = map.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"---"+value);
        }
        System.out.println("---------------------------");
        //foreach()遍历，已经完成集合遍历，获取到键和值，传给BiComsumer接口的accept方法
        //accept方法做什么等我们实现
        map.forEach((k,v)-> System.out.println(k+"---"+v));*/
        //TreeMap集合
//        TreeMap<Student, String> tm = new TreeMap<>((o1,o2)->{
//            int result = o1.getAge()-o2.getAge();
//            result = result == 0? o1.getName().compareTo(o2.getName()):result;
//            return result;
//        });
//        Student s1 = new Student("赵子龙",23);
//        Student s2 = new Student("王也",20);
//        Student s3 = new Student("黄飞鸿",21);
//        Student s4 = new Student("黄飞鸿",21);
//        Student s5 = new Student("陈真",20);
//        tm.put(s1,"常山");
//        tm.put(s2,"武当");
//        tm.put(s3,"佛山");
//        tm.put(s4,"佛山");
//        tm.put(s5,"精武");
//        tm.forEach((k,v)-> System.out.println(k+"---"+v));
        //案例：键盘录入字符串，打印字符以及出现的次数
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入一段文本：");
        String str = sc.nextLine();
        HashMap<Character,Integer> hm = new HashMap<>();
        char[] chars = str.toCharArray();
        for (char c : chars) {
            if(!hm.containsKey(c)){
                hm.put(c,1);
            }else {
                hm.put(c,hm.get(c)+1);
            }
//            hm.put(c,hm.containsKey(c)?hm.get(c)+1:1);
        }
        hm.forEach((k,v)->System.out.println(k+"出现的次数是："+v));
    }
}

class Student{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }
}